package com.appview;

import com.dao.TeacherDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Qi_Huang on 2015/11/2.
 */
public class AddCourse extends JFrame implements ActionListener {
    private JComboBox jcbDay, jcbTime, jcbTeacher;
    private JTextField jtPlace, jtName;
    private JLabel jlDay, jlTime, jlTeacher, jlPlace, jlName;
    private JPanel jPanel1, jPanel2, jPanel3, jPanel4, jPanel5, jPanel6;
    private JButton btnAdd, btnBack;
    private int row, col;
    private String teacher;

    public AddCourse(int row, int col, String teacher) {
        this.row = row;
        this.col = col;
        this.teacher = teacher;
    }



    public void openView() {
        initComponent();
//        jPanel1.add(jlDay);
//        jPanel1.add(jcbDay);
//        jPanel2.add(jlTime);
//        jPanel2.add(jcbTime);
        jPanel3.add(jlTeacher);
        jPanel3.add(jcbTeacher);
        jPanel4.add(jlPlace);
        jPanel4.add(jtPlace);
        jPanel5.add(btnAdd);
        jPanel5.add(btnBack);
        jPanel6.add(jlName);
        jPanel6.add(jtName);

//        jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
//        jPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel4.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel5.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel6.setLayout(new FlowLayout(FlowLayout.CENTER));
//
//        add(jPanel1);
//        add(jPanel2);
//        add(jPanel3);
        add(jPanel4);
        add(jPanel6);
        add(jPanel5);

        setLayout(new GridLayout(3, 1));
        setSize(250, 250);
        setLocation(400, 200);
        setVisible(true);
        setTitle("添加课程");
        setAlwaysOnTop(true);
    }

    public void initComponent() {
//        jlDay = new JLabel("星期");
//        jlTime = new JLabel("时间");
        jlTeacher = new JLabel("教师");
        jlPlace = new JLabel("课室");
        jlName = new JLabel("课程");
        jtName = new JTextField(8);
        jtPlace = new JTextField(5);
//        jcbDay = new JComboBox(Config.DAY1_LIST);
//        jcbTime = new JComboBox(Config.CLASS_LIST);
        jcbTeacher = new JComboBox(TeacherDao.getInstance().getTList());

        btnAdd = new JButton("添加");
        btnBack = new JButton("返回");
        btnAdd.addActionListener(this);
        btnBack.addActionListener(this);
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jPanel3 = new JPanel();
        jPanel4 = new JPanel();
        jPanel5 = new JPanel();
        jPanel6 = new JPanel();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnAdd) {
            //添加
            String s = getInfo();
            try {
                TableInfoTools.addCourse(row, col, s);
            } catch (ArrayIndexOutOfBoundsException e1) {
                JOptionPane.showMessageDialog(this, "未选中一个单元格", "Error", JOptionPane.WARNING_MESSAGE);
            } finally {
                this.dispose();
            }
            this.dispose();
        } else {
            this.dispose();
        }
    }

    //获取到添加窗口的信息
    private String getInfo() {
        String info ="";
        info += jtName.getText() + " ";
        info += teacher + " ";
        info += jtPlace.getText().toString();
        info = info.trim();
        info = info.replaceAll("\\s+", "<br>");
        info = "<html>" + info;

        return info;
    }


}
